package com.cspg.design.presenter;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.cspg.design.CSPGApplication;
import com.cspg.design.MainActivity;
import com.cspg.design.bean.deviceBean.PointDeviceBean;
import com.cspg.design.dao.gen.DaoSession;
import com.cspg.design.model.LocalStorageModel;
import com.cspg.design.request.LocalStorage;
import com.cspg.design.ui.login.LoginActivity;
import com.cspg.design.widget.CSPGProgressBar;
import com.cspg.design.widget.ToastUtils;

import java.io.File;
import java.util.List;

public class UploadFilePresenter implements IPresenter {

    private CSPGProgressBar cspgProgressBar;
    private final LocalStorage localStorageModel;

    private final Activity activity;
    private final DaoSession daoSession;
    private PointDeviceBean pointDeviceBean;

    private List<String> imgFlag;

    public UploadFilePresenter(Activity activity) {
        this.activity = activity;
        this.daoSession = ((CSPGApplication) activity.getApplication()).getDaoSession();
        localStorageModel = new LocalStorage(this);
    }


    /**
     * 这种上传方式支持单张图片和多张图片，同时也支持上传除了图片类的其他文件
     *
     * @param files
     * @return
     */
    public void uploadFile(List<File> files, String folderName) {
        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            localStorageModel.upload(files, folderName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 多图上传
     */
    public void uploadMultipartBodyFile(List<File> files, PointDeviceBean pointDeviceBean, List<String> imgFlag) {

        if (cspgProgressBar == null) {
            cspgProgressBar = CSPGProgressBar.show(activity);
        } else {
            cspgProgressBar.show();
        }
        try {
            this.pointDeviceBean = pointDeviceBean;
            this.imgFlag = imgFlag;
            localStorageModel.uploadFiles(files);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getFailure(String msg) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, msg);
    }

    public void getTokenExpireFailure() {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        Intent intent = new Intent();
        intent.setClass(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    public void requestSuccess(List<LocalStorageModel> localStorageModelList) {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        if (localStorageModelList != null && this.pointDeviceBean != null) {
            {
                if (localStorageModelList.size() == imgFlag.size()) {
                    for (int i = 0; i < imgFlag.size(); i++) {
                        String img = imgFlag.get(i);
                        switch (img) {
                            case "far":
                                pointDeviceBean.setFarImage(localStorageModelList.get(i).getRealName());
                                break;
                            case "near":
                                pointDeviceBean.setNearImage(localStorageModelList.get(i).getRealName());
                                break;
                            case "pole":
                                pointDeviceBean.setPoleImage(localStorageModelList.get(i).getRealName());
                                break;
                        }
                    }
                }
                daoSession.getPointDeviceBeanDao().insertOrReplace(pointDeviceBean);
                Toast.makeText(this.activity, "更新成功。。。。", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void netWorkError(String netError) {
//        activity.hideProgress();
//        activity.toast(netError);

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, netError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestFailure(String noDataError) {
//        activity.hideProgress();
////        activity.toast(noDataError);
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, noDataError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestSuccess() {
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
    }

    public PointDeviceBean getPointDeviceBean() {
        return this.pointDeviceBean;
    }
}